﻿ 
using SHKF.B2C.Model;
using SHKF.B2C.Model.Entity.Center;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text; 

namespace SHKF.B2C.BLL.Center
{
    public class ShopMapBLL : CenterBLL
    {

        public ShopMapBLL(SqlSugarClient db) : base(db)
        {

        }

        public int Save(ShopMapEntity shopMap)
        {
            int result = 0;
          
            if (shopMap.SN == 0)
            {
                result = this.db
                    .Insertable<ShopMapEntity>(shopMap)
                    .ExecuteCommand();
            }
            else
            {
                result = this.db
                    .Updateable<ShopMapEntity>(shopMap) 
                    .ExecuteCommand();
            }
            return result;
        }

        public void DeleteShop(long ShopSN)
        {
            this.db
                .Deleteable<ShopMapEntity>() 
                .Where(oo=>oo.ShopSN == ShopSN)
                .ExecuteCommand();
        }

        public void Delete(long SN)
        {
            this.db
                .Deleteable<ShopMapEntity>()
                .In(SN) 
                .ExecuteCommand();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shopSN">虚拟门店SN</param>
        /// <param name="platform"></param>
        /// <returns></returns>
        public ShopMapEntity Get(int shopSN, string platform)
        {
            ShopMapEntity shop = this.db
                .Queryable<ShopMapEntity>()
                .Where(oo => oo.ShopSN == shopSN && oo.Platform == platform)
                .First();
             
            return shop;
        }


        /// <summary>
        /// 获取有赞云总部id
        /// </summary>
        /// <param name="companySN"></param>
        /// <returns></returns>
        public long GetKdtId(long companySN)
        {
            ShopMapEntity map = this.db
                                  .Queryable<ShopMapEntity>()
                                  .Where(oo => oo.CompanySN == companySN && oo.ShopSN == 0 && oo.Platform == PlatformEnum.YouZanYun)
                                  .First();

            if (map != null)
            {
                return map.app_poi_code;
            }

            return 0;

        }

        public long GetKdtIdOfShopSN(long ShopSN)
        {
           long CompanySN =  this.db
                .Queryable<ShopEntity>()
                .Where(ww => ww.ShopSN == ShopSN)
                .Select(col=>col.CompanySN)
                .First();

            ShopMapEntity map = this.db
                                 .Queryable<ShopMapEntity>()
                                 .Where(oo => oo.CompanySN == CompanySN && oo.ShopSN == 0 && oo.Platform == PlatformEnum.YouZanYun)
                                 .First();

            if (map != null)
            {
                return map.app_poi_code;
            }

            return 0;
        }

        /// <summary>
        /// 通过第三方ID，获取平台ShopSN
        /// </summary>
        /// <param name="app_poi_code"></param>
        /// <returns></returns>
        public ShopMapEntity GetShop(long app_poi_code, string Platform)
        {
            ShopMapEntity shopMap = this.db
                                 .Queryable<ShopMapEntity>()
                                 .Where(oo => oo.app_poi_code == app_poi_code && oo.Platform == Platform)
                                 .First(); 
            return shopMap;
        }

        public List<ShopMapEntity> Query(long shopSN, string platform)
        {
            var cmd = this.db
                .Queryable<ShopMapEntity>();

            if (shopSN >= 0)
            {
                cmd.Where(oo => oo.ShopSN == shopSN);
            }

            if (!string.IsNullOrEmpty(platform))
            {
                cmd.Where(oo => oo.Platform == platform);
            }

            List<ShopMapEntity> list = cmd.ToList(); 
            return list;
        }

        /// <summary>
        /// 虚拟店在平台下是否配置
        /// </summary>
        /// <param name="shopVSN"></param>
        /// <param name="platform"></param>
        /// <returns></returns>
        public bool IsExist(long shopVSN, string platform)
        {
           bool bExist = this.db
                .Queryable<ShopMapEntity>()
                .Where(ww => ww.ShopSN == shopVSN && ww.Platform == platform)
                .Any();

            return bExist;
        }
    }
}
